﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;


using Cards.Util;
using Cards.Objects;

namespace Cards.GUI
{
    public partial class INS_Card : UserControl
    {
        private CCard m_card;
        private int x;
        private int y;        
        private int m_width;
        private int m_height;
        private Image m_picPath;
        private PictureBox m_pic;
        private int m_select;
        private INS_Player m_insPlayer;

        public int X
        {
            get { return x; }
            set { x = value; }
        }

        public int Y
        {
            get { return y; }
            set { y = value; }
        }
        public int WIDTH
        {
            get { return m_width; }
            set { m_width = value; }
        }
        public int HEIGHT
        {
            get { return m_height; }
            set { m_height = value; }
        }

        public int SELECT
        {
            get { return m_select; }
            set { m_select = value; }
        }

        public Image PICPATH
        {
            get { return m_picPath; }
            set { m_picPath = value; }
        }

        public PictureBox PIC
        {
            get { return m_pic; }
            set { m_pic = value; }
        }

        public CCard CARD
        {
            get { return m_card; }
            set { m_card = value; }
        }

        public INS_Player IP
        {
            get { return m_insPlayer; }
            set { m_insPlayer = value; }
        }

        public INS_Card(int _x, int _y, int _cardType, Image _picPath, int _width, int _height,
                            INS_Player _insPlayer)
        {
            InitializeComponent();

            m_select = -1;
            this.x = _x ;
            this.y = _y ;
            m_width = _width;
            m_height = _height;
            this.m_picPath = _picPath;
            this.m_card = new CCard(this, _cardType);
            m_insPlayer = _insPlayer;
        }

        public INS_Card(INS_Card _card)
        {
            InitializeComponent();

            m_select = _card.SELECT;
            this.x = _card.X;
            this.y = _card.Y;
            m_width = _card.WIDTH;
            m_height = _card.HEIGHT;
            this.m_picPath = _card.PICPATH;
            this.PIC = _card.PIC;
            this.m_card = _card.CARD;
            m_insPlayer = _card.IP;
        }

        public void Init()
        {
            this.Size = new Size(new Point(m_width, m_height));
            this.Location = new Point(x, y);

            m_pic = new PictureBox();
            m_pic.Size = new Size(Constants.s_cardWidth, Constants.s_cardHeight);
            m_pic.BackColor = Color.Transparent;
            
            if (!Environment.CurrentDirectory.Equals(MainFrame.sCurDir))
            {
                Environment.CurrentDirectory = MainFrame.sCurDir;
            }
        }

        public void CreateGUI()
        {
            Init();

            Bitmap bmp = new Bitmap(m_picPath);

            if (m_pic.Image != null)
            {
                m_pic.Image.Dispose();
            }

            m_pic.Image = (Bitmap)bmp;
            m_pic.SizeMode = PictureBoxSizeMode.StretchImage;

            //if (!MainFrame.sPlayNextState)
            //{
                m_pic.MouseDown += new MouseEventHandler(INS_Card_MouseDown);
                this.Controls.Add(m_pic);
            //}
        }

        private void INS_Card_MouseDown(object sender, MouseEventArgs e)
        {
            if (!this.m_card.PLAY && INS_Stage.sSide == this.IP.STT &&
                INS_Stage.sPlay && !(MainFrame.s_gameKind == CARD_PLAYING_KIND.BLACKJACK))
            {
                SelectCard();
            }
        }

        //Do slecting card
        public void SelectCard()
        {
            if (!this.m_card.PLAY)
            {
                this.m_select = -this.m_select;
                this.Top = this.Top - Constants.s_mobility * this.m_select;
                if (m_select == 1)
                {
                    m_pic.BackColor = Color.AliceBlue;
                }
                else if (m_select == -1)
                {
                    m_pic.BackColor = Color.Transparent;
                }
            }
        }

        public void Play()
        {
            MoveCard();
            m_card.PLAY = true;

            //Hien quan bai danh. Giau quan bai
            if (MainFrame.s_multiType == MULTI_PLAYER_TYPE.SINGLE_PLAYER)
            {
                if (m_insPlayer.PLAYERKIND == PLAYER_TYPE.COMPUTER)
                {
                    BringToFront();
                    Visible = true;
                }
            }
            else
            {
                if ((m_insPlayer.PLAYERKIND == PLAYER_TYPE.COMPUTER) ||
                    ((IP.PLAYERKIND == PLAYER_TYPE.HUMAN)
                    && (IP.STT != MainFrame.m_frmMultiGame.GAMEPLAYERID - 1)))
                {
                    BringToFront();
                    Visible = true;
                }
            }
        }

        public void MoveCard()
        {
            this.Left = x;
            this.Top = y;
        }

        public void HideCards()
        {
            this.Visible = false;
        }
    }
}
